// pages/themeAvatar/themeAvatar.js
let http = require('../../utils/api.js');
let domain = require('../../utils/api.js');
import {
    toData,
    myHttps,
    json,
    login
} from '../../utils/util.js';
import {
    myHttpss,
} from '../../utils/json.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        images: [], //所有头像列表
        domain: '', //ip
        choose: false, //控制小对号得显示与隐藏
        editor: '', //从编辑主题页面进来得标识
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            domain: domain.domain,
            editor: options.index
        })
        this.themeAvatar()
    },
    // 头像列表
    themeAvatar() {
        let url = http.api.themeAvatar;
        myHttps({
            url: url,
            method: "GET",
        }).then(res => {
            if (res.code == 1) {
                this.setData({
                    images: res.data
                })
            }
        }).catch(err => {})
    },
    // 点击某张图片
    choose(e) {
        // console.log(e)
        let index = e.currentTarget.dataset.index
        let url = e.currentTarget.dataset.item

        wx.setStorage({ //把选择得头像存下来
            key: 'avatar',
            data: url,
        })
        if (this.data.editor == 'editor') {
            wx.redirectTo({
                url: '../EditorTheme/EditorTheme?index=' + this.data.editor,
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
            })
        } else {
            wx.redirectTo({
                url: '../createTheme/createTheme',
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
            })
        }
    },
})